const { exec } = require('./sql')

const fs = require('fs');
const join = require('path').join;
const path = require('path');

function readAllSqlFile(filePath) {
    let sqlFiles = [];
    function findFile(p) {
        let files = fs.readdirSync(p);
        files.forEach(function (item, index) {
            let fPath = join(p, item);
            let stat = fs.statSync(fPath);
            if (stat.isDirectory() === true) {
                findFile(fPath);
            }
            if (stat.isFile() === true) {
                var ext = path.extname(fPath);
                if (ext === '.sql') {
                    sqlFiles.push(fPath);
                }
            }
        });
    }
    findFile(filePath);
    return sqlFiles;
}

const files = readAllSqlFile("./init/");

function start() {
    files.forEach((filePath) => {
        const stringObject = fs.readFileSync(filePath, "utf-8");
        const sql_list = stringObject.split(";");
        sql_list.forEach(async (sql) => {
            if (sql.trim() !== "") {
                await exec(sql);
            }
        });
    });
}

start();

